package com.projectzqjz.parttimework.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.projectzqjz.parttimework.App;
import com.projectzqjz.parttimework.R;
import com.projectzqjz.parttimework.adapter.Part3JobPubAdapter;
import com.projectzqjz.parttimework.adapter.PartApplyAdapter;
import com.projectzqjz.parttimework.base.LazyLoadBaseFragment;
import com.projectzqjz.parttimework.entity.DailyEntity;
import com.projectzqjz.parttimework.entity.PartBaseEntity;
import com.projectzqjz.parttimework.net.MyCallback;
import com.projectzqjz.parttimework.net.Task;
import com.projectzqjz.parttimework.ui.activity.PartDetailActivity;
import com.projectzqjz.parttimework.utils.SPUtils;
import com.projectzqjz.putorefresh.BaseBothListener;
import com.projectzqjz.putorefresh.PullToBothLayout;
import com.projectzqjz.putorefresh.ViewStatus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Response;

@SuppressLint("ValidFragment")
public class PartApplyFragment extends LazyLoadBaseFragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.pullRefresh)
    PullToBothLayout pullToBothLayout;
    private String type;
    private Part3JobPubAdapter dailyAdapter = null;
    private List<DailyEntity> list = new ArrayList<>();
    private boolean isRefresh = true;
    private int page = 1;
    private String flag;

    public PartApplyFragment(String type) {
        this.type = type;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_part_apply;
    }

    @Override
    protected void initView(View rootView) {
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        if (dailyAdapter == null) {
            dailyAdapter = new Part3JobPubAdapter(list);
        }
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        // 第一种，直接取消动画
        RecyclerView.ItemAnimator animator = recyclerView.getItemAnimator();
        if (animator instanceof SimpleItemAnimator) {
            ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
        }
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(dailyAdapter);
        dailyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //跳转兼职详情
                startActivity(new Intent(getActivity(), PartDetailActivity.class).putExtra("jobId", list.get(position).getJobId()));
            }
        });
        setListen();
    }

    private void setListen() {

        pullToBothLayout.setRefreshListener(new BaseBothListener() {
            @Override
            public void refresh() {
                page = 1;
                isRefresh = true;
                loadData();
            }

            @Override
            public void loadMore() {
                page++;
                isRefresh = false;
                loadData();
            }
        });
    }

    //关闭下拉刷新和上拉加载
    private void closePullTo() {
        pullToBothLayout.finishRefresh();
        pullToBothLayout.finishLoadMore();
    }

    /**
     * 只进行刷新一次
     */
    @Override
    protected void requestData() {
        super.requestData();
        switch (type) {
            case "1"://全部
                flag = "";
                loadData();
                break;
            case "2"://已报名
                flag = "-1";
                loadData();
                break;
            case "3"://进行中
                flag = "0";
                loadData();
                break;
            case "4"://已完成
                flag = "1";
                loadData();
                break;
        }
    }

    //加载数据
    private void loadData() {
        if (isRefresh)
            pullToBothLayout.showView(ViewStatus.LOADING_STATUS);
        String userId= SPUtils.get(App.getContext(),"userId","")+"";
        Task.getApiService().getMyApply(userId, flag, page).enqueue(new MyCallback<PartBaseEntity>(getActivity()) {
            @Override
            protected void onSuccess(Response<PartBaseEntity> response) {
                assert response.body() != null;
                String jsonStr = JSONObject.toJSONString(response.body().getData());
                JSONArray jsonArray = JSONArray.parseArray(jsonStr);
                if (isRefresh) {
                    list.clear();
                    if (jsonArray.size() > 0) {
                        pullToBothLayout.showView(ViewStatus.CONTENT_STATUS);
                        for (int i = 0; i < jsonArray.size(); i++) {
                            DailyEntity dailyEntity = new DailyEntity();
                            dailyEntity.setTitle(jsonArray.getJSONObject(i).getString("title"));
                            dailyEntity.setJobId(jsonArray.getJSONObject(i).getInteger("id") + "");
                            dailyEntity.setLabel(jsonArray.getJSONObject(i).getString("jobPayType"));
                            dailyEntity.setMoney(jsonArray.getJSONObject(i).getInteger("jobPay") + "");
                            dailyEntity.setUnit(jsonArray.getJSONObject(i).getString("jobPayUnit"));
                            dailyEntity.setGsName(jsonArray.getJSONObject(i).getString("jobCompany"));
                            dailyEntity.setLocation(jsonArray.getJSONObject(i).getString("jobAddress"));
                            dailyEntity.setTime(jsonArray.getJSONObject(i).getString("time"));
                            list.add(dailyEntity);
                        }

                    } else {
                        pullToBothLayout.showView(ViewStatus.EMPTY_STATUS);
                    }
                } else {
                    pullToBothLayout.showView(ViewStatus.CONTENT_STATUS);
                    if (jsonArray.size() > 0) {
                        for (int i = 0; i < jsonArray.size(); i++) {
                            DailyEntity dailyEntity = new DailyEntity();
                            dailyEntity.setTitle(jsonArray.getJSONObject(i).getString("title"));
                            dailyEntity.setJobId(jsonArray.getJSONObject(i).getInteger("id") + "");
                            dailyEntity.setLabel(jsonArray.getJSONObject(i).getString("jobPayType"));
                            dailyEntity.setMoney(jsonArray.getJSONObject(i).getInteger("jobPay") + "");
                            dailyEntity.setUnit(jsonArray.getJSONObject(i).getString("jobPayUnit"));
                            dailyEntity.setGsName(jsonArray.getJSONObject(i).getString("jobCompany"));
                            dailyEntity.setLocation(jsonArray.getJSONObject(i).getString("jobAddress"));
                            dailyEntity.setTime(jsonArray.getJSONObject(i).getString("time"));
                            list.add(dailyEntity);
                        }
                    }
                }
                dailyAdapter.notifyDataSetChanged();
                closePullTo();
            }


            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }

}
